package com.ruoyi.project.module.api.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.module.api.annotation.Login;
import com.ruoyi.project.module.api.annotation.LoginUser;
import com.ruoyi.project.module.api.common.utils.ApiResult;
import com.ruoyi.project.module.api.service.NoticeServicePlus;
import com.ruoyi.project.module.common.dict.AppOrderStatus;
import com.ruoyi.project.module.common.dict.SysNoticeType;
import com.ruoyi.project.module.member.domain.Member;
import com.ruoyi.project.module.order.domain.Order;
import com.ruoyi.project.module.order.service.IOrderService;
import com.ruoyi.project.system.notice.domain.Notice;
import com.ruoyi.project.system.notice.service.INoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/order")
@Api("前台订单接口")
public class AppOrderController extends BaseController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private NoticeServicePlus noticeService;

    @Login
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation("报告查询列表")
    public TableDataInfo list(@LoginUser Member member, Order order) {
        order.setMemberId(member.getId());
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    @Login
    @PostMapping("/applyInvoice/{orderId}")
    @ResponseBody
    @ApiOperation("申请发票")
    public ApiResult applyInvoice(@LoginUser Member member, @PathVariable Long orderId) {
        String now = DateUtil.formatDateTime(new Date());
        Notice notice = new Notice();
        String noticeTitle = "【%s】会员申请发票通知";
        notice.setNoticeTitle(String.format(noticeTitle, now));
        notice.setNoticeType(SysNoticeType.D_3_XX);
        String noticeContent = "%s，会员【%s】正在申请订单号为【%s】的发票，请及时处理！";
        notice.setNoticeContent(String.format(noticeContent, now, member.getName(), orderId));
        noticeService.insertNotice(notice);
        return ApiResult.ok();
    }

}
